package cn.amebius.simpledocker.handler;

import cn.amebius.simpledocker.enums.ErrorCode;
import cn.amebius.simpledocker.exception.*;
import cn.amebius.simpledocker.util.Response;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import javax.servlet.ServletException;
import javax.ws.rs.ProcessingException;
import java.io.IOException;
import java.net.ConnectException;

/**
 * 统一异常处理器
 * @ControllerAdvice 配合 @ExceptionHandler 实现全局异常处理
 * @author 阿伟
 * @date 2023/7/27 20:27
 */
@RestControllerAdvice
@Slf4j
public class CommandExceptionHandler {

    {
        log.info("CommandExceptionHandler:启动");
    }

    @ExceptionHandler
    public HttpEntity<Response<?>> handlerExceptions(Exception e) {
        String msg = "未知错误";
        ErrorCode errorCode = ErrorCode.SYSTEM_ERROR;
        if (e instanceof  RuntimeException) {
            if (e instanceof ProcessingException) {
                log.error("ProcessingException通讯错误异常: {}", e.getMessage());
                msg = "Docker服务连接异常";
                errorCode = ErrorCode.DOCKER_ENDPOINT_CONNECT_ERROR;
            } else if (e instanceof LoginFailException) {
                log.error("LoginFailException登录失败异常: {}", e.getMessage());
                msg = e.getMessage();
                errorCode = ErrorCode.LOGIN_FAIL;
            } else if (e instanceof ParameterErrorException) {
                log.error("ParameterErrorException参数错误异常: {}", e.getMessage());
                msg = e.getMessage();
                errorCode = ErrorCode.PARAM_ERROR;
            } else if (e instanceof OperationErrorException) {
                log.error("OperationErrorException操作错误异常: {}", e.getMessage());
                msg = e.getMessage();
                errorCode = ErrorCode.PARAM_ERROR;
            } else if (e instanceof SystemErrorException) {
                log.error("SystemErrorException系统错误异常: {}", e.getMessage());
                msg = e.getMessage();
                errorCode = ErrorCode.SYSTEM_ERROR;
            } else if (e instanceof DataErrorException) {
                log.error("DataErrorException数据错误异常: {}", e.getMessage());
                msg = e.getMessage();
                errorCode = ErrorCode.DATA_ERROR;
            } else if (e instanceof DockerErrorException) {
                log.error("DockerErrorException主机错误异常: {}", e.getMessage());
                msg = e.getMessage();
            } else {
                log.error("RuntimeException异常: {}", e.getMessage());
                msg = e.getMessage();
                e.printStackTrace();
            }
        } else if (e instanceof ServletException) {
            log.error("ServletException异常：{}",e.getMessage());
            msg = e.getMessage();
        }
//        e.printStackTrace();
        return new HttpEntity<>(Response.fail(msg).setErrorCode(errorCode));
    }
    public static SaResult handleSaTokenException(Throwable e) {
        log.error("SaTokenException错误异常:{}",e.getMessage());
//        e.printStackTrace();
        return SaResult.error().set("msg",e.getMessage()).set("code",ErrorCode.ERROR_TOKEN.getCode());
    }

}
